#!/usr/bin/env python3

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; specifically version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2016
# Author: Cleber Rosa <cleber@redhat.com>

#
# Simple script that, given Python file containing unittests, returns the canonical
# location of each test, each one on its own line. The idea is to be able
# to filter the tests you want to run by doing something like:
#
# $ avocado run `avocado-safeloader-find-python-unittests <path-to-test.py> | <your-condition> | xargs`
#

import os
import sys
from functools import cmp_to_key
from unittest.util import three_way_cmp

from avocado.core.safeloader import find_python_unittests

if __name__ == '__main__':
    test_module_paths = sys.argv[1:]
    result = []
    for test_module_path in test_module_paths:
        try:
            test_class_methods = find_python_unittests(test_module_path)
        except IOError as error:
            continue
        for klass, methods in test_class_methods.items():
            if test_module_path.endswith(".py"):
                test_module_path = test_module_path[:-3]
            test_module_name = os.path.relpath(test_module_path)
            test_module_name = test_module_name.replace(os.path.sep, ".")
            result += ["%s.%s.%s" % (test_module_name, klass, method_name)
                       for (method_name, _, _) in methods]
    if result:
        if os.environ.get('AVOCADO_SAFELOADER_UNITTEST_ORDER_COMPAT', False):
            result.sort(key=cmp_to_key(three_way_cmp))
        print("\n".join(result))
